<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2019/6/18
  Time: 17:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="cn.dgut.Utils.JDBCUtils" %>
<%@ page import="cn.dgut.Utils.JDBCUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="cn.dgut.Entity.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
    JDBCUtils utils = new JDBCUtils();

    //接收数据
    String pages = request.getParameter("page");
    String key = request.getParameter("key");
    String show = request.getParameter("showPage");
    int showPage,maxPage=1;
    int count=0,pageSize=5;

    if(show==null){
        showPage = 1;
    }else {
        showPage = Integer.parseInt(show);
    }

    List<Object> list = new ArrayList<>();
    List<String> titles = new ArrayList<>();
    List<String> changelistname = new ArrayList<>();
    List<String> changelist = new ArrayList<>();
    String table = "";
    String pageTitle = "";
    String page_btn = "";

    if(pages!=null){
        if(pages.equals("0")){
            pageSize = 8;
            pageTitle = "用户注册";
            titles.add("id");
            titles.add("名字");
            titles.add("电话");
            titles.add("邮箱");
            changelist.add("名字");
            changelist.add("密码");
            changelist.add("电话");
            changelist.add("邮箱");
            changelistname.add("name");
            changelistname.add("passwd");
            changelistname.add("phone");
            changelistname.add("email");
            if(key==null){
                list = utils.queryAllData("User",showPage,pageSize);
            }else {
                list = utils.queryAllData("User",key);
            }

            for (int i = 0; i < list.size(); i++) {
                User user = (User) list.get(i);
                table=table+"<tr><td>"+user.getUser_id()+"</td><td>"+user.getUser_name()+
                        "</td><td>"+user.getUser_phone()+"</td><td>"+user.getUser_email()+"</td><td>\n" +
                        "                            <a href=\"#\" class=\"handle-change\" data-id=\""+user.getUser_id()+"\"data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-pencil\"></i> 修改</a>\n" +
                        "                            <span style=\"width: 10px;\"></span>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+user.getUser_id()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("User");
        }else if(pages.equals("1")){
            pageSize = 8;
            pageTitle = "学员信息";
            titles.add("id");
            titles.add("名字");
            titles.add("电话");
            titles.add("邮箱");
            titles.add("VIP");
            titles.add("学历");
            changelist.add("名字");
            changelist.add("密码");
            changelist.add("电话");
            changelist.add("邮箱");
            changelist.add("VIP");
            changelist.add("学历");
            changelistname.add("name");
            changelistname.add("passwd");
            changelistname.add("phone");
            changelistname.add("email");
            changelistname.add("vip");
            changelistname.add("level");

            if(key==null){
                list = utils.queryAllData("Student",showPage,pageSize);
            }else {
                list = utils.queryAllData("Student",key);
            }
            for (int i = 0; i < list.size(); i++) {
                Student student = (Student) list.get(i);
                table=table+"<tr><td>"+student.getStudent_id()+"</td><td>"+student.getStudent_name()+
                        "</td><td>"+student.getStudent_phone()+"</td><td>"+student.getStudent_email()+
                        "</td><td>"+student.getIsVip()+"</td><td>"+student.getSlevel()+
                        "</td><td>\n" +
                        "                            <a href=\"#apps\" class=\"handle-change\" data-id=\""+student.getStudent_id()+"\" data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-pencil\"></i> 修改</a>\n" +
                        "                            <span style=\"width: 10px;\"></span>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+student.getStudent_id()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("Student");
        }else if(pages.equals("2")){
            pageSize = 5;
            pageTitle = "教员信息";
            titles.add("名字");
            titles.add("电话");
            titles.add("邮箱");
            titles.add("学历");
            titles.add("底薪");
            titles.add("简介");
            changelist.add("名字");
            changelist.add("密码");
            changelist.add("电话");
            changelist.add("邮箱");
            changelist.add("学历");
            changelist.add("底薪");
            changelist.add("简介");
            changelistname.add("name");
            changelistname.add("passwd");
            changelistname.add("phone");
            changelistname.add("email");
            changelistname.add("level");
            changelistname.add("wage");
            changelistname.add("info");

            if(key==null){
                list = utils.queryAllData("Teacher",showPage,pageSize);
            }else {
                list = utils.queryAllData("Teacher",key);
            }
            for (int i = 0; i < list.size(); i++) {
                Teacher teacher = (Teacher) list.get(i);
                table=table+"<tr><td>"+teacher.getTeacher_name()+
                        "</td><td>"+teacher.getTeacher_phone()+"</td><td>"+teacher.getTeacher_email()+
                        "</td><td>"+teacher.getTlevel()+"</td><td>"+teacher.getWage()+"</td><td class='text-left'>"+teacher.getTinfo()+
                        "</td><td>\n" +
                        "                            <a href=\"#apps\" class=\"handle-change\"  data-id=\""+teacher.getTeacher_id()+"\" data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-pencil\"></i> 修改</a>\n" +
                        "                            <span style=\"width: 10px;\"></span>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+teacher.getTeacher_id()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("Teacher");
        }else if(pages.equals("3")){
            pageSize = 5;
            pageTitle = "课程列表";
            titles.add("状态");
            titles.add("课程名");
            titles.add("老师");
            titles.add("课时");
            titles.add("类型");
            titles.add("开始时间");
            titles.add("学费");
            titles.add("简介");

            changelist.add("课程名");
            changelist.add("老师");
            changelist.add("课时");
            changelist.add("类型");
            changelist.add("开始时间");
            changelist.add("学费");
            changelist.add("简介");
            changelist.add("状态");

            changelistname.add("name");
            changelistname.add("teacher");
            changelistname.add("hours");
            changelistname.add("type");
            changelistname.add("begin_time");
            changelistname.add("pay");
            changelistname.add("info");
            changelistname.add("isSell");
            if(key==null){
                list = utils.queryAllData("Course",showPage,pageSize);
            }else {
                int flag=1;
                List templist = utils.queryAllData("Teacher");
                List courses = utils.queryAllData("Course");

                for (int i = 0; i < templist.size(); i++) {
                    Teacher temp = (Teacher) templist.get(i);
                    if(temp.getTeacher_name().equals(key.trim())){
                        flag=0;
                        for (int j = 0; j < courses.size(); j++) {
                            Course temp1 = (Course) courses.get(j);
                            if (temp1.getTid() == temp.getTeacher_id()) {
                                list.add(temp1);
                            }
                        }
                    }
                }
                if(key.contains("在售")){
                    flag=0;
                    for (int j = 0; j < courses.size(); j++) {
                        Course temp1 = (Course) courses.get(j);
                        if (temp1.getIsSell() == 1) {
                            list.add(temp1);
                        }
                    }
                }
                if(key.contains("下架")){
                    flag=0;
                    for (int j = 0; j < courses.size(); j++) {
                        Course temp1 = (Course) courses.get(j);
                        if (temp1.getIsSell() == 0) {
                            list.add(temp1);
                        }
                    }
                }
                if(flag==1) list = utils.queryAllData("Course",key);
            }

            for (int i = 0; i < list.size(); i++) {
                Course Course = (Course) list.get(i);
                Teacher tea = (Teacher) utils.queryOneData("Teacher",Course.getTid());
                table=table+"<tr><td>"+(Course.getIsSell()==1?"在售中":"已下架")+"</td><td>"+Course.getCourse_name()+"</td><td>"+tea.getTeacher_name()+
                        "</td><td>"+Course.getHours()+"</td><td>"+Course.getType()+
                        "</td><td>"+Course.getCourse_begin()+"</td><td>"+Course.getCourse_pay()+"</td><td class='text-left'>"+Course.getCinfo()+
                        "</td><td>\n" +
                        "                            <a href=\"#apps\" class=\"handle-change\"  data-id=\""+Course.getCourse_id()+"\" data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-pencil\"></i> 修改</a>\n" +
                        "                            <span style=\"width: 10px;\"></span>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+Course.getCourse_id()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("Course");
        }else if(pages.equals("4")){
            pageSize = 8;
            pageTitle = "报名列表";
            titles.add("课程名");
            titles.add("学员");
            titles.add("报名时间");
            changelist.add("课程名");
            changelist.add("学员");
            changelist.add("报名时间");
            changelistname.add("cname");
            changelistname.add("student");
            changelistname.add("time");
            if(key==null){
                list = utils.queryAllData("Select_Course",showPage,8);
            }else {
                int flag = 1;
                List courses = utils.queryAllData("Course");
                List sclist = utils.queryAllData("Select_Course");
                for (int i = 0; i < courses.size(); i++) {
                    Course temp = (Course) courses.get(i);
                    if(temp.getCourse_name().contains(key)){
                        flag=0;
                        for (int i1 = 0; i1 < sclist.size(); i1++) {
                            Select_Course sc = (Select_Course) sclist.get(i1);
                            if(sc.getCourse_id()==temp.getCourse_id())
                                list.add(sc);
                        }
                    }
                }
                List stulist = utils.queryAllData("Student");
                for (int i = 0; i < stulist.size(); i++) {
                    Student temp = (Student) stulist.get(i);
                    if(temp.getStudent_name().contains(key)){
                        flag=0;
                        for (int i1 = 0; i1 < sclist.size(); i1++) {
                            Select_Course sc = (Select_Course) sclist.get(i1);
                            if(sc.getSid()==temp.getStudent_id())
                                list.add(sc);
                        }
                    }
                }
                if(flag==1)
                    list = utils.queryAllData("Select_Course",key);
            }

            for (int i = 0; i < list.size(); i++) {
                Select_Course Select_Course = (Select_Course) list.get(i);
                Course course = (Course) utils.queryOneData("Course",Select_Course.getCourse_id());
                Student student = (Student) utils.queryOneData("Student",Select_Course.getSid());
                table=table+"<tr><td>"+course.getCourse_name()+"</td><td>"+student.getStudent_name()+
                        "</td><td>"+Select_Course.getTime()+
                        "</td><td>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+Select_Course.getId()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("Select_Course");
        }else if(pages.equals("5")){
            pageSize = 5;
            pageTitle = "学生作品";
            titles.add("学员");
            titles.add("浏览次数");
            titles.add("发布时间");
            titles.add("内容");
            changelist.add("学员");
            changelist.add("内容");
            changelistname.add("name");
            changelistname.add("info");

            if(key==null){
                list = utils.queryAllData("StudentWorks",showPage,pageSize);
            }else {
                list = utils.queryAllData("StudentWorks",key);
            }

            for (int i = 0; i < list.size(); i++) {
                StudentWorks StudentWorks = (StudentWorks) list.get(i);
                table=table+"<tr><td>"+StudentWorks.getStudent_name()+"</td><td>"+StudentWorks.getViews()+
                        "</td><td>"+StudentWorks.getTime()+"</td><td class='text-left'> ...... </td><td>\n" +
                        "                            <a href=\"#apps\" class=\"handle-change\" data-id=\""+StudentWorks.getStudentWorks_id()+"\" data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-pencil\"></i> 修改</a>\n" +
                        "                            <span style=\"width: 10px;\"></span>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+StudentWorks.getStudentWorks_id()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("StudentWorks");
        }else if(pages.equals("6")){
            pageSize = 5;
            pageTitle = "招聘列表";
            titles.add("标题");
            titles.add("联系电话");
            titles.add("底薪");
            titles.add("详情");
            changelist.add("标题");
            changelist.add("联系电话");
            changelist.add("底薪");
            changelist.add("详情");
            changelistname.add("title");
            changelistname.add("phone");
            changelistname.add("wage");
            changelistname.add("info");

            if(key==null){
                list = utils.queryAllData("Recruit",showPage,pageSize);
            }else {
                list = utils.queryAllData("Recruit",key);
            }

            for (int i = 0; i < list.size(); i++) {
                Recruit Recruit = (Recruit) list.get(i);
                table=table+"<tr><td>"+Recruit.getTitle()+"</td><td>"+Recruit.getPhone()+
                        "</td><td>"+Recruit.getWage()+"</td><td class='text-left'>"+Recruit.getRinfo()+
                        "</td><td>\n" +
                        "                            <a href=\"#apps\" class=\"handle-change\" data-id=\""+Recruit.getRecruit_id()+"\" data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-pencil\"></i> 修改</a>\n" +
                        "                            <span style=\"width: 10px;\"></span>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+Recruit.getRecruit_id()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("Recruit");
        }else if(pages.equals("7")){
            pageSize = 5;
            pageTitle = "简历列表";
            titles.add("名字");
            titles.add("电话");
            titles.add("邮箱");
            titles.add("学历");
            titles.add("底薪");
            titles.add("简介");
            changelist.add("名字");
            changelist.add("密码");
            changelist.add("电话");
            changelist.add("邮箱");
            changelist.add("学历");
            changelist.add("底薪");
            changelist.add("简介");
            changelistname.add("name");
            changelistname.add("passwd");
            changelistname.add("phone");
            changelistname.add("email");
            changelistname.add("level");
            changelistname.add("wage");
            changelistname.add("info");

            if(key==null){
                list = utils.queryAllData("Resume",showPage,pageSize);
            }else {
                list = utils.queryAllData("Resume",key);
            }
            for (int i = 0; i < list.size(); i++) {
                Teacher teacher = (Teacher) list.get(i);
                table=table+"<tr><td>"+teacher.getTeacher_name()+
                        "</td><td>"+teacher.getTeacher_phone()+"</td><td>"+teacher.getTeacher_email()+
                        "</td><td>"+teacher.getTlevel()+"</td><td>"+teacher.getWage()+"</td><td class='text-left'>"+teacher.getTinfo()+
                        "</td><td>\n" +
                        "                            <a href=\"#apps\" class=\"handle-change\"  data-id=\""+teacher.getTeacher_id()+"\" data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-pencil\"></i> 修改</a>\n" +
                        "                            <span style=\"width: 10px;\"></span>\n" +
                        "                            <a href=\"/DeleteList?page="+pages+"&id="+teacher.getTeacher_id()+"\" class=\"handle-del\"><i class=\"fa fa-trash-o\"></i> 删除</a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
            }
            count = utils.count("Resume");
        }else {
            table+="<h3>访问出错！！！</h3>";
        }

        //计算页数
        if(count%pageSize==0){
            maxPage = count/pageSize;
        }else {
            maxPage = count/pageSize + 1;
        }

        if(key==null){
            if(showPage!=1){
                page_btn = "<a class='btn-add' href='main_page.jsp?page="+pages+"&showPage=1'>首页</a>&nbsp;";
                page_btn += "<a class='btn-add' href='main_page.jsp?page="+pages+"&showPage="+((showPage-1)>0?(showPage-1):1)+"'>&laquo; 上一页</a>&nbsp;";
            }
            if(showPage!=maxPage&&maxPage!=0){
                page_btn += "<a class='btn-add' href='main_page.jsp?page="+pages+"&showPage="+((showPage+1)<maxPage?(showPage+1):maxPage)+"'>下一页 &raquo;</a>&nbsp;";
                page_btn += "<a class='btn-add' href='main_page.jsp?page="+pages+"&showPage="+maxPage+"'>尾页</a>&nbsp;";
            }
            if(page_btn!=""){
                page_btn = "<div style='width:180px;margin:30px auto;'>"+page_btn+"</div>";
            }
        }
    }else {
        table+="<h3>访问出错！！！</h3>";
    }
%>